{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4239a68e",
   "metadata": {},
   "source": [
    "# correlation\n",
    "1. formula\n",
    "$$\n",
    "    corr(X,Y) = \\frac{cov(X,Y)}{\\sigma_{X}\\sigma_{Y}} = \\frac{\\Sigma_{i}^{n}{(X_i-\\bar X)(Y_i- \\bar Y)}}{\\sqrt{\\Sigma_{i}^{n}{(X_i-\\bar X)^2}}\\sqrt{\\Sigma_{i}^{n}{(Y_i-\\bar Y)^2}}}\n",
    "$$\n",
    "2. numpy knowledge\n",
    "    > 1. one-dimensional array could not use T (transpose) method, use reshape(n,-1) instead <br>\n",
    "    > 2. broadcast functionality\n",
    "    > 3. <font color=red> np.cov( ) np.corrcoef(  ) </font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a6f7bfe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "from sklearn.datasets import load_iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e775adfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(150, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# generate a set of data\n",
    "iris_data, iris_label = load_iris(return_X_y=True)\n",
    "iris_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "dd08738d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.68112222, -0.04215111,  1.26582   ,  0.51282889],\n",
       "       [-0.04215111,  0.18871289, -0.32745867, -0.12082844],\n",
       "       [ 1.26582   , -0.32745867,  3.09550267,  1.286972  ],\n",
       "       [ 0.51282889, -0.12082844,  1.286972  ,  0.57713289]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# covariance calculation\n",
    "\n",
    "iris_miu = iris_data - iris_data.mean(axis=0)\n",
    "iris_cov = np.dot(iris_miu.T,iris_miu)/iris_data.shape[0]\n",
    "iris_cov"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "84862fc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.11756978,  0.87175378,  0.81794113],\n",
       "       [-0.11756978,  1.        , -0.4284401 , -0.36612593],\n",
       "       [ 0.87175378, -0.4284401 ,  1.        ,  0.96286543],\n",
       "       [ 0.81794113, -0.36612593,  0.96286543,  1.        ]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# correlation calculation\n",
    "# use broadcast feature, vertically once and horizontally once\n",
    "\n",
    "iris_sigma = np.diag(iris_cov)\n",
    "iris_std = np.sqrt(iris_sigma)\n",
    "iris_corr = np.divide(np.divide(iris_cov,iris_std),iris_std.reshape(4,-1))\n",
    "iris_corr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1eacfb9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.68569351, -0.042434  ,  1.27431544,  0.51627069],\n",
       "       [-0.042434  ,  0.18997942, -0.32965638, -0.12163937],\n",
       "       [ 1.27431544, -0.32965638,  3.11627785,  1.2956094 ],\n",
       "       [ 0.51627069, -0.12163937,  1.2956094 ,  0.58100626]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# using function method\n",
    "np.cov(iris_data.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "fde29608",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.11756978,  0.87175378,  0.81794113],\n",
       "       [-0.11756978,  1.        , -0.4284401 , -0.36612593],\n",
       "       [ 0.87175378, -0.4284401 ,  1.        ,  0.96286543],\n",
       "       [ 0.81794113, -0.36612593,  0.96286543,  1.        ]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# using function method\n",
    "np.corrcoef(iris_data.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bd84c5d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3.6",
   "language": "python",
   "name": "python3.6"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
